#include <iostream>
#include <thread>
#include <vector>
#include <unistd.h>
#define NUM 3//线程数量
using namespace std;

int val = 10;
thread_local int t_val = 0;

void fun(string name)
{
   while(1)
   {
        if(val > 0)
        {
            usleep(10000);
            cout << name << " get one" << " total: " << t_val << " val: " << val <<endl;
            val--;
            t_val++;
        }
        else 
        {
            break;
        }
   }

}

int main()
{
    thread trd[NUM];
    //创建多个线程
    for(int i = 0; i < NUM; i++)
    {
        string name = "thread" + to_string(i+1);
        trd[i] = thread(fun, name);
    }
    //等待线程
    for(int i = 0; i < NUM; i++)
    {
        trd[i].join();
    }
    return 0;
}